//
//  WYTabBarController.m
//  WYModel
//
//  Created by ios on 2017/3/2.
//  Copyright © 2017年 Wy. All rights reserved.
//

#import "WYTabBarController.h"
#import "WYNavigationController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "SearchVC.h"

@interface WYTabBarController ()

/**
 启动图
 */
@property(nonatomic,strong)UIImageView *launchImage;


@end

@implementation WYTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setLaunch];
    [self addChildVC];
}

-(void)setLaunch{
    self.launchImage       = [[UIImageView alloc] initWithFrame:self.view.frame];
  //self.launchImage.image = [UIImage imageNamed:@""];
    self.launchImage.image = [UIImage createImageWithColor:[UIColor cyanColor]
                                                     frame:self.launchImage.frame];
    self.launchImage.tag   = 100;
    [self.view addSubview:self.launchImage];
    //延迟三秒后启动图消失
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self endLaunch];
    });
    
    UIButton  *btn         = [[UIButton alloc] buttonWithTitle:@"跳过" ImageName:nil Frame:CGRectMake(kWidth-80, 80, 40, 30)];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    btn.layer.cornerRadius = 15;
    btn.layer.borderWidth  = 0.6;
    btn.tag                = 101;
    btn.layer.borderColor  = [UIColor blackColor].CGColor;
    
    [btn addTarget:self action:@selector(endLaunch) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}
-(void)endLaunch{
    if (![self.view viewWithTag:100]) {
        return;
    }
    UIView *btn = [self.view viewWithTag:101];
    if (btn) {
        [btn removeFromSuperview];
    }
    
    WS(weakSelf);
    [UIView animateWithDuration:1.3 animations:^{
        weakSelf.launchImage.transform = CGAffineTransformMakeScale(1.3, 1.3);
        weakSelf.launchImage.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf.launchImage removeFromSuperview];
    }];
}

//添加子控制器
-(void)addChildVC{
    OneViewController *oneVc = [[OneViewController alloc] init];
    WYNavigationController *homeNav  = [self setChildVc:oneVc title:@"首页" imageName:@"" withSelectedName:@""];
    //依次添加
    //""""""""""
    TwoViewController *twoVc = [[TwoViewController alloc] init];
    WYNavigationController *twoNav  = [self setChildVc:twoVc title:@"二页" imageName:@"" withSelectedName:@""];
    
    ThreeViewController *threeVc = [[ThreeViewController alloc] init];
    WYNavigationController *threeNav  = [self setChildVc:threeVc title:@"三页" imageName:@"" withSelectedName:@""];
    
    SearchVC *searchVc = [[SearchVC alloc] init];
    WYNavigationController *searchNav  = [self setChildVc:searchVc title:@"四页" imageName:@"" withSelectedName:@""];
    
    self.viewControllers = @[homeNav,twoNav,threeNav,searchNav];
}

-(WYNavigationController *)setChildVc:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName withSelectedName:(NSString *)selectedName{
    vc.title                    = title;
    vc.tabBarItem.image         = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedName];
    vc.tabBarController.tabBar.tintColor = THEME_COLOR;
    
    NSDictionary  *dic          = @{NSForegroundColorAttributeName:BLACK_COLOR,NSFontAttributeName:([UIFont systemFontOfSize:12])};
    NSDictionary  *selectDic    = @{NSForegroundColorAttributeName:THEME_COLOR,NSFontAttributeName:([UIFont systemFontOfSize:12])};
    
    [vc.tabBarItem setTitleTextAttributes:dic forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:selectDic forState:UIControlStateSelected];
    
    WYNavigationController *nav = [[WYNavigationController alloc] initWithRootViewController:vc];
   
    return nav;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
